from cold_start_analyzer.base_analyzer import BaseAnalyzer
from cold_start_analyzer.consts import NOT_AVAILABLE
from cold_start_analyzer import db_helper
from cold_start_analyzer.log_helper import dlog


class Phase6All(BaseAnalyzer):
    description = "视频播放: 从 H:doRequest 结束到 H:video_play 开始"
    """
    起点： app_thread
    H:doRequest 结束
    终点： app_thread
    H:video_play 开始
    """
    def get_result(self):
        if 'Phase5All' not in self.context:
            dlog.warning(f"Phase5All not in context: {self.context}")
            return NOT_AVAILABLE
        res = db_helper.get_function_cost(self.cursor, 'H:video_play', self.context['Phase5All']['end_ts']-1, None, thread_name=self.thread_name)
        if not res:
            dlog.warning(f"video_play marker not found for thread {self.thread_name}")
            return NOT_AVAILABLE
        self.context['Phase6All'] = {
            'start_ts': self.context['Phase5All']['end_ts'],
            'end_ts': res[0][0],
        }
        return res[0][0] - self.context['Phase5All']['end_ts']
